package scala

import org.apache.spark.{SparkConf, SparkContext}

/**
  * 项目名称:spark-learn<br>
  * 包名:scala<br>
  * 用于:spark-learn<br>
  * 创建时间:2019年03月16日<br>
  * 更新时间:2019年03月16日<br>
  *
  * @author :lds（创建人）<br>
  * @version :v1.0（版本号）<br>
  * @since jdk1.8
  */
@SerialVersionUID(1L)
object CollaborativeFilteringSpark{


  /**
    * 因为有序列化问题，所以把方法都定义在mainh函数里面
    * @param args
    */
  def main(args: Array[String]): Unit = {

    def getSource():Map[String,Map[String,Int]] = {
      val user1FilmSource:Map[String,Int] = Map("smzdm"->2,"ylxb"->3,"znh"->1,"nhsc"->0,"fcwr"->1)
      val user2FilmSource = Map("smzdm"->1,"ylxb"->2,"znh"->2,"nhsc"->1,"fcwr"->4)
      val user3FilmSource = Map("smzdm"->2,"ylxb"->1,"znh"->0,"nhsc"->1,"fcwr"->4)
      val user4FilmSource = Map("smzdm"->3,"ylxb"->2,"znh"->0,"nhsc"->5,"fcwr"->3)
      val user5FilmSource = Map("smzdm"->5,"ylxb"->3,"znh"->1,"nhsc"->1,"fcwr"->2)
      var source = Map[String,Map[String,Int]]()
      source += ("aaa" -> user1FilmSource)
      source += ("bbb" -> user2FilmSource)
      source += ("ccc" -> user3FilmSource)
      source += ("ddd" -> user4FilmSource)
      source += ("eee" -> user5FilmSource)
      source
    }

    def getCollaborateSource(user1:String,user2:String):Double={
      val source = getSource()
      val user1FilmSource = source.get(user1).get.values.toVector
      val user2FilmSource = source.get(user2).get.values.toVector
      //计算余弦相识度上半部分
      val member = user1FilmSource.zip(user2FilmSource).map(d => d._1 * d._2).sum.toDouble
      //计算余弦相识度下半部分
      //分母第一个变量
      val temp1 = math.sqrt(user1FilmSource.map(num=>{math.pow(num,2)}).sum)
      //分母第二个变量
      val temp2 = math.sqrt(user2FilmSource.map(num=>{math.pow(num,2)}).sum)
      //求出分母
      val denominator = temp1*temp2
      denominator
    }


    val conf = new SparkConf()
    conf.setMaster("local").setAppName("textFile")
    val sc = new SparkContext(conf)
    val users = sc.parallelize(Array("aaa","bbb","ccc","ddd","eee"))
    val films = sc.parallelize(Array("smzdm","ylxb","znh","nhsc","fcwr"))

    //设定目标
    val name = "bbb"
    users.foreach(user => {println(name+" 相对于 "+user+"的相似性分数是："+getCollaborateSource(name,user))})

  }
}
